package com.huike.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huike.pojo.Drug;
import com.huike.pojo.Register;
import com.huike.service.RegisterService;
import com.huike.vo.ExportExcel;
import com.huike.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("register")
public class RegisterController {
    @Autowired
    RegisterService registerService;
    @RequestMapping("addRegister")
    public ResultVO<Void> addRegister(Register register){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss");
        String msg = sdf.format(date);
        register.setRegisterDate(msg);
        register.setSelfPrice(0);
        Integer row =  registerService.addRegister(register);
        if(row>=1){
            return new ResultVO<>(200,"挂号成功");
        }
        return new ResultVO<>(500,"挂号时发生未知异常");
    }
    @RequestMapping(value = "getRegisters")
    public ResultVO<PageInfo> getRegisters(Integer id, String registerName, String deptName, String star, String end,
                                           @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                           @RequestParam(value="pageSize",defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Register> registers = registerService.getRegisters(id,registerName,deptName,star,end);
        PageInfo pageInfo = new PageInfo(registers, 5);
        return new ResultVO<PageInfo>(pageInfo);
    }

    @RequestMapping("blackRegister")
    public ResultVO<Void> blackRegister(Integer[] ids){
        Integer row=0;
        for(Integer id:ids){
            registerService.rUpdateStatus(id,-1);
            row++;
        }
        if(row==ids.length){
            return new ResultVO<>(200,"退号成功");
        }
        return new ResultVO<>(500,"退号发生未知异常");
    }

    @RequestMapping("updateRegister")
    public ResultVO<Void> updateRegister(Register register){
        Integer row = registerService.updateRegister(register);
        if(row>=1){
            return new ResultVO<>(200,"修改成功");
        }
        return new ResultVO<>(500,"修改时发生未知异常");
    }

    @RequestMapping("getAllRegister")
    public List<Register> getAllRegister(){
        return registerService.getAllRegister();
    }

    @RequestMapping("excelInfo")
    public ResultVO<Void> excelInfo(String outStream){
        List<Register> list = registerService.getAllRegister();
        String[] headerStr = {"门诊编号","病人姓名","身份证号","挂号费","社保号", "手机号",
                "是否自费","性别","年龄","职业","是否复诊","挂号门诊",
                "主治医生","备注","挂号日期","状态"};
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String fileName=outStream +"\\"+ sdf.format(new Date())+"挂号表.xls";
        ExportExcel exportExcel = new ExportExcel();
        try{
            OutputStream outputStream = new FileOutputStream(fileName);
            exportExcel.exportExcel(list,outputStream,headerStr);
        }catch (Exception e){
            return new ResultVO<>(500,"导出时未知异常");
        }
        return new ResultVO<>(200,"导出成功");
    }
}
